package com.webshop.DataBase;

import com.webshop.JavaBean.Order;
import com.webshop.Utils.ServletOut;
import com.webshop.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
    OrderService os = new OrderService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String method = request.getParameter("method");
        System.out.println(method);
        switch (method) {
            case "ShowOrder":
                showOrder(request, response);
                break;
            case "finish":
                 finish(request,response);
                 break;
            case "unfinish":
                unfinish(request,response);
                break;
            default:
                     break;
            }

    }

    /**
     * 返回全部订单
     * @param request
     * @param response
     */
    private void showOrder(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String name = String.valueOf(session.getAttribute("name"));
        List<Order> temp = os.showOrder(name);
        request.setAttribute("order", temp);
        System.out.println(temp.toString());

        ServletOut.request(request,response,"/html/dingdan.jsp");
    }

    /**
     * 已完成订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void finish(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String name = String.valueOf(session.getAttribute("name"));
        System.out.println(name);
        List<Order> temp =new ArrayList<>();
        try {
            temp= os.finsh(name);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("flse");
        }

        req.setAttribute("finishorder", temp);
        System.out.println(temp.toString());
        ServletOut.request(req,resp,"/html/history-order.jsp");
    }

    /**
     * 未完成订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void unfinish(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String name = String.valueOf(session.getAttribute("name"));
        System.out.println(name);

        List<Order> temp =new ArrayList<>();
        try {
            temp= os.unfinsh(name);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("flse");
        }
        req.setAttribute("unfinishorder", temp);
        System.out.println(temp.toString());
        ServletOut.request(req,resp,"/html/weizhifu.jsp");
    }
}
